package com.hsy.dao.impl;

import com.hsy.dao.BaseDao;
import com.hsy.dao.UserDao;
import com.hsy.pojo.User;

import java.time.LocalDateTime;
import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {

    /**
     * 根据用户账号查询用户
     * @param userNumber
     * @return 用户
     */
    @Override
    public User selectUserByNumber(String userNumber) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user" +
                " where user_number = ?";
        User user =this.getBean(sql, userNumber);
        return user;
    }

    /**
     * 根据用户昵称查询用户
     * @param userName
     * @return 用户
     */
    @Override
    public User selectUserByNick(String userName) {
        String sql = "select user_id, dept_id, user_number, " +
                "user_name, password, telephone, status, avatar_address" +
                ", create_time, update_time from user" +
                " where user_name = ?";
        User user =this.getBean(sql, userName);
        return user;
    }

    /**
     * 插入用户
     * @param user
     */
    @Override
    public int insert(User user) {
        String sql = "insert into user(user_number, user_name, password, telephone, create_time, avatar_address) values(?, ?, ?, ?, ?, ?)";
        int update = this.update(sql, user.getUserNumber(), user.getUserName(), user.getPassword(),
                                        user.getTelephone(), user.getCreateTime(), user.getAvatarAddress());
        return update;
    }

    /**
     * 根据用户账号查询数据库中的密码
     * @param userNumber
     * @return 真实用户密码
     */
    @Override
    public String selectUserPasswordByNumber(String userNumber) {
        String sql = "select password from user where user_number = ?";
        User user =this.getBean(sql, userNumber);
        return user.getPassword();
    }

    @Override
    public List<User> selectUserList() {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user";
        List<User> users = this.getBeanList(sql);
        return users;
    }

    @Override
    public User selectUserByUserId(Long userId) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user" +
                " where user_id = ?";
        User user =this.getBean(sql, userId);
        return user;
    }

    @Override
    public int update(User user) {
//        String sql = "update user set user_number as userNumber = ?,user_name as userName = ?, dept_id as deptId = ?, telephone = ?, status = ? WHERE user_id = ?";
        // 更新不需要重命名，是跟新表里的数据
        String sql = "UPDATE user \n" +
                "SET user_number = ?, user_name = ?, dept_id = ?, telephone = ?, status = ? \n" +
                "WHERE user_id = ?;\n";
        int update = this.update(sql, user.getUserNumber(), user.getUserName(), user.getDeptId(),
                user.getTelephone(), user.getStatus(), user.getUserId());
        return update;
    }

    @Override
    public int delete(Long userId) {
        String sql = "delete from user WHERE user_id = ?";
        int update = this.update(sql, userId);
        return update;
    }

    @Override
    public List<User> selectUserListByStatus(String status) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user where status = ?";
        List<User> users = this.getBeanList(sql, status);
        return users;
    }

    @Override
    public List<User> selectUserListByName(String nickName) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user where user_name like concat('%', ?, '%')";
        List<User> users = this.getBeanList(sql, nickName);
        return users;
    }

    @Override
    public List<User> selectUserListByNameAndStatus(String nickName, String status) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user where user_name like concat('%', ?, '%') and status = ?";
        List<User> users = this.getBeanList(sql, nickName, status);
        return users;
    }

    @Override
    public List<User> selectUserList(Integer startIndex, Integer targetTotal) {
        String sql = "select user_id as userId, dept_id as deptId, user_number as userNumber, " +
                "user_name as userName, password, telephone, status, avatar_address as avatarAddress" +
                ", create_time as createTime, update_time as updateTime from user" +
                " limit ?, ?";
        List<User> users = this.getBeanList(sql, startIndex, targetTotal);
        return users;
    }
}
